package com.example.dellpc.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.fragment.ActivityFragment;
import com.example.fragment.HomeFragment;
import com.example.fragment.MeFragment;
import com.example.fragment.ProductFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 刘智强 on 2016/8/8.
 */
public class TablLayoutActivity extends FragmentActivity {


    private ViewPager viewPager;
    private MyViewPagerAdapter viewPagerAdapter;
    private TabLayout tabLayout;
    private static String[] titles = new String[]{"首页", "产品", "活动", "我的"};
    private Fragment home_fragment, product_fragmengt, activity_fragment, me_fragment;
    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detailactivity);
        initView();
    }

    public void initView() {
        initFragments();
        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        initTabs();
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position=tab.getPosition();
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //失去选中
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //连续点击

            }
        });
    }


    public class MyViewPagerAdapter extends FragmentStatePagerAdapter {

        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (!fragments.isEmpty()) {
                return fragments.get(position);
            }
            return null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public int getCount() {
            return titles.length;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    public void initTabs() {
        for (int i = 0; i < titles.length; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(titles[i]).setIcon(getResources().getDrawable(R.mipmap.ic_launcher)));
        }
//            TabLayout.Tab tab = tabLayout.getTabAt(i);
//            View v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.view_tab, null);
//            ImageView iv = (ImageView) v.findViewById(R.id.iv_icon);
//            TextView tv = (TextView) v.findViewById(R.id.tv_title);
//            iv.setImageResource(R.mipmap.ic_launcher);
//            tv.setText(titles[i]);
//            if (tab != null) {
//                tab.setCustomView(v);
//            }
//
//        }
    }

    public void initFragments() {
        fragments = new ArrayList<>();
        home_fragment = new HomeFragment();
        product_fragmengt = new ProductFragment();
        activity_fragment = new ActivityFragment();
        me_fragment = new MeFragment();
        fragments.add(home_fragment);
        fragments.add(product_fragmengt);
        fragments.add(activity_fragment);
        fragments.add(me_fragment);


    }
}
